#ifndef LOCK_MANAGER
#define LOCK_MANAGER


#include <map>
#include <pthread.h>
#include "DataStructures/Resource.h"
#include "CommunicationModule/DlmComm.h"
#include "CommunicationModule/Request.h"
#include "CommunicationModule/Response.h"
#include "LockManagerModule/TimeoutManager.h"


/*
 * Class processing requests from clients
 */	
class LockManager
{
	public:
		/*
		 * Consturctor
		 * cm	- communication module used to get requests and send responses
		 */		
		LockManager(DlmComm *cm);
		
		~LockManager();
		
		/*
		 * Starts lock manager
		 */			
		void run();
		
		/*
		 * Used to inform lock manager that client with pid waiting for some resource
		 * should be unblocked because of timeout
		 */			
		void timedOut(pid_t pid);
		
		/*
		 * Used to inform lock manager that client has exited and all his
		 * locks should be removed
		 */		
		void exited(pid_t pid);
				
	private:
		typedef std::map<int, Resource*> Resources;
		
		TimeoutManager timeoutManager_;
		Resources resources_;
		DlmComm *dlmComm_;		
		
		pthread_mutex_t mutex_;		
			
		void processRequest(const LockRequest *request);
		void processRequest(const TryLockRequest *request);
		void processRequest(const UnlockRequest *request);	
		
		void handleWaiting();				
};

#endif
